package com.jintian.smart.kernel.orm.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.jintian.smart.kernel.core.domain.ITailEntity;
import com.jintian.smart.kernel.core.orm.IBaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.beetl.core.fun.MethodInvoker;
import org.beetl.core.fun.ObjectUtil;
import org.beetl.sql.annotation.entity.AssignID;
import org.beetl.sql.annotation.entity.Column;
import org.beetl.sql.annotation.entity.Version;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 实体类的基类
 */

@Data
public abstract class BaseBaseEntity implements IBaseEntity, ITailEntity, Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    /**
     * 主键id
     */
    @Schema(description = "主键id")
    @Column(name = "id", comment = "主键")
    @AssignID("snowid")
    private String id;

    /**
     * 主键id
     */
    @Schema(description = "版本号")
    @Column(name = "version", comment = "版本号")
    @Version(1)
    private Long version;

    @Schema(description = "操作方式")
    @Column(name = "operate", comment = "操作方式")
    private String operate;

    @JsonIgnore
    private Map<String, Object> _extAttrs = new HashMap<>();

    @Override
    public Map<String, Object> extAttrs() {
        return _extAttrs;
    }

    public void setDynamicValue(String attrName, Object value) {
        MethodInvoker methodInvoker = ObjectUtil.getInvokder(this.getClass(), attrName);
        if (methodInvoker == null) {
            throw new IllegalArgumentException("不存在的属性 " + attrName);
        }
        methodInvoker.set(this, value);
    }

    public Object getDynamicValue(String attrName) {
        MethodInvoker methodInvoker = ObjectUtil.getInvokder(this.getClass(), attrName);
        if (methodInvoker == null) {
            throw new IllegalArgumentException("不存在的属性 " + attrName);
        }
        return methodInvoker.get(this);
    }
}
